<?php

namespace App\Http\Controllers\Client\Activity\Talent;

use App\Http\Controllers\ClientController;
use App\Models\Activity;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use App\Models\Tools\Period;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ActivityListController extends ClientController
{

    public function index()
    {
        $all = $this->all;

        if (!isset($all['city'])) {
            return $this->setReturnJson(-1, '参数错误');
        }

        $pageNo = $all['pageNo'] ?? 1;
        $pageSize = $all['pageSize'] ?? 10;

        try {
            // 查询所有我已经中奖过的、我已经参与未开奖的奖品，并排除
            $all['wined_shop_id'] = Activity::winedShopIds($this->we_id);
            $all['participating_shop_id'] = Activity::participatingShopIds($this->we_id);

            //查所有进行中的活动
            $collection = DB::table('lottery_activity as la')
                ->leftJoin('lottery_goods as lg', 'la.goods_id', '=', 'lg.goods_id')
                ->leftJoin('shop', 'la.shop_id', '=', 'shop.shop_id')
                ->leftJoin('dict_area as da', 'shop.district', '=', 'da.id')
                ->leftJoin('lottery_users_draw as lud', function ($join) {
                    $join->on('la.act_id', '=', 'lud.act_id')->where('lud.we_id', '=', $this->we_id);
                })
                ->where(function ($query) use ($all) {
                    $query->where('shop.city', '=', $all['city']);
                    $query->where('la.status', '=', Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_BEFORE);//正在进行中的霸王餐活动
                    $query->where('la.is_stop', '=', 0);

                    if (isset($all['district']) && $all['district'] !== 0) {
                        $query->where('shop.district', '=', $all['district']);
                    }
                    if (isset($all['type']) && $all['type'] !== 0) {
                        $query->where('shop.shop_type', '=', $all['type']);
                    }
                    if (isset($all['participate']) && $all['participate'] !== 0) {
                        // 查询所有参与过的活动
                        $participate_act_ids = DB::table('lottery_users_draw as lud')
                            ->leftJoin('lottery_activity as la', 'lud.act_id', '=', 'la.act_id')
                            ->where('lud.we_id', '=', $this->we_id)
                            ->where('la.status', '=', Data::DATA_DB_LOTTERY_ACTIVITY_STATUS_BEFORE) //正在进行的活动
                            ->pluck('lud.act_id')
                            ->toArray();
                        if ($all['participate'] === 1) { // 可参与
                            $query->whereNotIn('la.act_id', $participate_act_ids);
                            $query->whereNotIn('la.shop_id', array_merge($all['wined_shop_id'], $all['participating_shop_id']));
                        } elseif ($all['participate'] === 2) { // 已参与
                            $query->whereIn('la.act_id', $participate_act_ids);
                        }
                    }
                })
                ->orderByDesc('la.act_id')
                ->select('la.act_id', 'la.act_no', 'la.goods_id', 'la.shop_id', 'la.start_time', 'la.end_time', 'la.status',
                    'lg.goods_name', 'lg.title_img', 'lg.price', 'shop.shop_name', 'da.short_name', 'shop.address', 'shop.shop_type',
                    'lud.participate_id', 'lud.status as participate_status')
                ->skip(($pageNo - 1) * $pageSize)
                ->take($pageSize)
                ->get();

            $act_ids = $collection->pluck('act_id')->toArray();
            $count = DB::table('lottery_users_draw')
                ->whereIn('act_id', $act_ids)
                ->groupBy('act_id')
                ->select('act_id')
                ->selectRaw('COUNT(*) as count')
                ->get()
                ->mapWithKeys(function ($item) {
                    return [$item->act_id => $item->count];
                })
                ->toArray();
            $count = json_decode(json_encode($count), true);

            $participate_number = DB::table('shop_type')
                ->select('st_id','participate_number')
                ->get()
                ->mapWithKeys(function($item){
                    return [$item->st_id => $item->participate_number];
                })
                ->toArray();

            $result = $collection->map(function ($item) use ($all, $count, $participate_number) {
                $item->wined = in_array($item->shop_id, $all['wined_shop_id']); // 是否赢得过这个奖品
                $item->pending = in_array($item->shop_id, $all['participating_shop_id']); // 是否已经正在参与这个奖品的其他抽奖
                $item->text = Activity::activityStatus($item->status, $item->participate_status, $item->wined, $item->pending); // 状态
                $item->status_name = Data::$lotteryActivityStatus[$item->status];
                $item->title_img = AliOss::getUrl($item->title_img);
                $item->count = $count[$item->act_id] ?? 0;
                $item->shop_type_name = Data::$laifeiShopTypes[$item->shop_type];
                $item->members = $participate_number[$item->shop_type];
                return $item;
            })->toArray();
            return $this->setReturnJson(0, 'success', $result);
        } catch (\Exception $e) {
            Log::error(ActivityListController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '查询失败');
        }

    }

}
